package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;

import ModelLayer.Room;

public class DBRoomBookingLine_Room implements IFDBRoomBookingLine_Room {
	private Connection con;

	public DBRoomBookingLine_Room() {
		con = DBConnection.getInstance().getDBcon();

	}
	
	@Override
	public int deleteRoomForRBL(int rId) {
		int rc = -1;

		String query = "DELETE FROM rbl_room WHERE r_Id = '" + rId + "'";
		System.out.println(query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception ex) {
			System.out.println("Delete exception in rbl_room db: " + ex);
		}
		return rc;
	}

	@Override
	public LinkedList<Room> getAllRoomForRBL(int rblId) {
		ResultSet results;
		LinkedList<Room> list = new LinkedList<Room>();
		String query = "SELECT rbl_Id, r_Id FROM rbl_room WHERE rbl_Id = '"
				+ rblId + "'";
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while (results.next()) {
				Room room = new Room();
				room = buildRoom(results);
				list.add(room);
			}
			stmt.close();
		} catch (Exception e) {
			System.out.println("Query exception - select: " + e);
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int insertRBL_Room(int rblId, int roomId) {
		int rc = -1;
		String query = "INSERT INTO rbl_room(rbl_Id, r_Id) VALUES('" + rblId
				+ "','" + roomId + "')";
		System.out.println("insert : " + query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
			con.commit();
		} catch (SQLException ex) {
			System.out.println("rbl_room is not inserted");
		}
		return rc;
	}

	@Override
	public int updateRBL_Room(int rblId, int roomId, int oldRblId, int oldroomId) {
		int rc = -1;
		String query = "UPDATE rbl_room SET " + "rbl_Id = '" + rblId + "',"
				+ "r_Id = '" + roomId + "'" + " WHERE rbl_Id = '" + oldRblId
				+ "' AND r_Id = '" + oldroomId + "'";
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception e) {
			System.out.println("Update exception in rbl_room db: " + e);
		}
		return rc;
	}

	@Override
	public int deleteAllRoomForRBL(int rblId) {
		int rc = -1;

		String query = "DELETE FROM rbl_room WHERE rbl_Id = '" + rblId + "'";
		System.out.println(query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception ex) {
			System.out.println("Delete exception in rbl_room db: " + ex);
		}
		return rc;
	}

	public Room buildRoom(ResultSet results) {
		Room room = new Room();
		try {
			room.setId(results.getInt("r_Id"));
		} catch (Exception e) {
			System.out.println("error in building the room object");
		}
		return room;
	}

}
